package com.zxx.xin.shorthand.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.HorizontalScrollView;
import com.zxx.xin.shorthand.R;
import com.zxx.xin.shorthand.database.bean.Daily;
import com.zxx.xin.shorthand.database.DatabaseHelper;
import com.zxx.xin.shorthand.helper.BitmapHleper;
import com.zxx.xin.shorthand.helper.DateHelper;
import com.zxx.xin.shorthand.helper.FileUtils;
import com.zxx.xin.shorthand.helper.ShareUtils;
import com.zxx.xin.shorthand.ui.widget.MultiRowTextView;
import com.zxx.xin.shorthand.ui.widget.VerticalTextView;
import java.io.File;

/**
 * Created by xin on 2018/3/18.
 */

public class DailyReadActivity extends AppCompatActivity implements View.OnClickListener {

    private Daily mDaily;
    private HorizontalScrollView mHorizontalScrollView;
    private View mShareView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_daily_read);
        mDaily = getIntent().getParcelableExtra("data");
        mHorizontalScrollView = (HorizontalScrollView) findViewById(R.id.scrollview);
        //默认滑动到最右边
        mHorizontalScrollView.post(new Runnable() {
            @Override
            public void run() {
                mHorizontalScrollView.fullScroll(HorizontalScrollView.FOCUS_RIGHT);
            }
        });

        findViewById(R.id.update).setOnClickListener(this);
        findViewById(R.id.share).setOnClickListener(this);

        mShareView = findViewById(R.id.content_share);

        LayerDrawable layerDrawable = new LayerDrawable(new Drawable[]{mHorizontalScrollView.getBackground(), getResources().getDrawable(R.drawable.white_mask)});
        mHorizontalScrollView.setBackground(layerDrawable);
    }

    @Override
    protected void onResume() {
        super.onResume();
        refreshDailyData();
    }

    private void refreshDailyData() {
        if (mDaily != null) {
            mDaily = DatabaseHelper.getInstance().queryDailyById(mDaily.getId());
            ((VerticalTextView) findViewById(R.id.time)).setText(DateHelper.getChineseTimeInMillis(mDaily.getMCreateTime()));
            ((MultiRowTextView) findViewById(R.id.content)).setText(mDaily.getMContent());
            ((MultiRowTextView) findViewById(R.id.title)).setText(mDaily.getMTitle());
            setBackGround();
        }
    }

    public static void startPage(Context context, Daily daily) {
        Intent intent = new Intent(context, DailyReadActivity.class);
        intent.putExtra("data", daily);
        context.startActivity(intent);
    }

    private void setBackGround() {
        Bitmap bitmap = BitmapHleper.getBitmapByFilePath(this, mDaily.getMBgBitmap(), R.drawable.write_bg);
        LayerDrawable layerDrawable = new LayerDrawable(new Drawable[]{new BitmapDrawable(bitmap), getResources().getDrawable(R.drawable.white_mask)});
        findViewById(R.id.scrollview).setBackground(layerDrawable);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

            case R.id.update:
                DailyWriteActivity.startPage(this, mDaily);
                break;

            case R.id.share:
                Bitmap bitmap = BitmapHleper.generateBitmapFromHorizontalScrollView(mHorizontalScrollView);
                File file = FileUtils.saveBitmapToSDCard(bitmap);
                ShareUtils.shareBitmap(this, file);
                break;
        }
    }
}
